package com.tangcco.lockwell.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.domain.Question;
import com.tangcco.lockwell.repository.QuestionMapper;
import com.tangcco.lockwell.service.QuestionService;
import com.tangcco.lockwell.service.SubjectService;
import com.tangcco.lockwell.service.TextContentService;
import com.tangcco.lockwell.viewmodel.admin.question.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*薛明宇*/
@Service
public class QuestionServiceImpl extends BaseServiceImpl<QuestionVm> implements QuestionService {

    private final QuestionMapper questionMapper;

    @Autowired
    public QuestionServiceImpl(QuestionMapper questionMapper) {
        super(questionMapper);
        this.questionMapper = questionMapper;

    }

    @Override
    public List<Question> page(QuestionPageRequestVM requestVM) {
        PageInfo<Question> pageInfo = PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
                questionMapper.page(requestVM));
        return pageInfo.getList();
    }

    @Override
    public QuestionVm selectById(Integer id) {
        return questionMapper.selectById(id);
    }

    //三表查询
    @Override
    public List<QuestionVm> selectAll(QuestionPageRequestVM questionPageRequestVM) {
        return questionMapper.selectAll(questionPageRequestVM);
    }

    @Override
    public QuestionVm selectByIds(Integer id) {
        return questionMapper.selectById(id);
    }
    @Override
    public Question selectBaseInfoById(Integer id){
        return questionMapper.selectBaseInfoById(id);
    }

    @Override
    public int updateByIdS(QuestionVMUpdate questionVm) {
        return questionMapper.updateByIdS(questionVm);
    }

    @Override
    public List<QuestionVm> selectAllOther(QuestionPageRequestOtherVM questionPageRequestOtherVM) {
        return questionMapper.selectAllOther(questionPageRequestOtherVM);
    }

    @Override
    public int insertQuestion(QuestionVMUpdate questionVMUpdate) {
        return questionMapper.insertQuestion(questionVMUpdate);
    }
    @Override
    public QuestionEditRequestVM selectQuestionById(Integer id) {
        Integer index = 1;
        QuestionVm qy = questionMapper.selectById(id);
//		System.out.println(qy.getTextContent());
        QuestionEditRequestVM qM = new QuestionEditRequestVM();
        qM.setId(qy.getId());
        if(qy.getScore() != null){
            qM.setScore(qy.getScore().toString());
        }
        qM.setQuestionType(qy.getQuestionTypeId());
        qM.setSubjectId(qy.getSubjectId());
        //设置题干部分
        String textContent = qM.getTextContent();
        List<String> list = new ArrayList<>();
        //设置题号 和 选项部分
        List<QuestionIndex> itemsAndIndex = new ArrayList<>();
//		QuestionIndex qi = new QuestionIndex();
        String textContent1 = qy.getTextContent();
        JSONArray jsonArray = JSONUtil.parseArray(textContent1);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject questionJson = jsonArray.getJSONObject(i);
            String qtitle = questionJson.getStr("qtitle");
            list.add(qtitle);
            // 创建QuestionIndex对象
            QuestionIndex qis = new QuestionIndex();
            qis.setIndex(index++); // 设置题号
            // 解析选项部分
            JSONArray itemsJsonArray = questionJson.getJSONArray("item");
            List<QuestionEditItemVM> items = new ArrayList<>();
            if (itemsJsonArray != null) {
                for (int j = 0; j < itemsJsonArray.size(); j++) {
                    JSONObject itemJson = itemsJsonArray.getJSONObject(j);
                    String prefix = itemJson.getStr("prefix");
                    String content = itemJson.getStr("content");
                    // 创建QuestionEditItemVM对象
                    QuestionEditItemVM itema = new QuestionEditItemVM();
                    itema.setPrefix(prefix);
                    itema.setContent(content);
                    items.add(itema);
                }
            }
            qis.setItems(items);
            itemsAndIndex.add(qis);
        }
        qM.setArticle(qy.getArticleContent());
        qM.setTextContent(JSONUtil.toJsonStr(list));
        qM.setItemsAndIndex(itemsAndIndex);
        qM.setPicture(qy.getPicture());
        qM.setArticleContent(qy.getArticleContent());
        return qM;
    }

    @Override
    public int inserts(Question question) {
        return questionMapper.inserts(question);
    }

    @Override
    public int selectCount() {
        return questionMapper.selectCount();
    }

}
